package com.hankol.message;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  富文本过滤工具
 * @author zjl
 * 19/6/21
 * @description:
 */
public class HtmlUtil {
    // 定义script的正则表达式
    private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>";
    // 定义style的正则表达式
    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>";
    // 定义HTML标签的正则表达式
//    private static final String regEx_html = "<[^>]>";
//    private static final String regEx_html_a = "</[^>]+";
    private static final String regEx_html_img_p = "(?!<img.*?>)<[^/].*?>";
    private static final String regEx_html_img_b = "(?!</img.*?>)</.*?>";

    //定义空格回车换行符
    private static final String regEx_space = "\\s*|\t|\r|\n";

    //HTML特殊字符转义
    public static String unescapeHtml(String htmlStr){
        return htmlStr;
        //return StringEscapeUtils.unescapeHtml4(htmlStr);
    }

    public static String Html2Txt(String htmlStr) {
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // script
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // style
        String regEx_html = "<[^>]+>"; // HTML tag
        String regEx_space = "\\s+|\t|\r|\n";// other characters

        Pattern p_script = Pattern.compile(regEx_script,
                Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll("");

        Pattern p_style = Pattern
                .compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll("");

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll("");

        Pattern p_space = Pattern
                .compile(regEx_space, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(htmlStr);
        htmlStr = m_space.replaceAll(" ");

        return unescapeHtml(htmlStr);

    }

    /**
     * 删除a标签，
     * @param htmlStr
     * @return
     * 过滤富文本
     */
    public static String delHTMLTag(String htmlStr) {
        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        // 过滤script标签
        htmlStr = m_script.replaceAll("");
        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        // 过滤style标签
        htmlStr = m_style.replaceAll("");

        //删除a标签



        return cleanA(htmlStr);

    }


    /**
     * @param htmlStr
     * @return
     * 过滤富文本
     */
    public static String cleanStyleJs(String htmlStr,boolean cleanA) {
        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        // 过滤script标签
        htmlStr = m_script.replaceAll("");

        // 过滤style标签
        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll("");

        // 过滤style样式
        String regEx = " style=\"(.*?)\"";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(htmlStr);
        if (m.find()) {
            htmlStr = m.replaceAll("");
        }
        // 过滤style样式1
        regEx = " style='(.*?)'";
        p = Pattern.compile(regEx);
        m = p.matcher(htmlStr);
        if (m.find()) {
            htmlStr = m.replaceAll("");
        }

        if(cleanA){
            return cleanA(htmlStr);
        }else{
            return htmlStr;
        }

    }

    public static String getTextFromHtml(String htmlStr){
        htmlStr = delHTMLTag(htmlStr);
        htmlStr = htmlStr.replaceAll("&nbsp;", "");
        htmlStr = htmlStr.replaceAll("&gt;", "");
        return htmlStr;
    }

    /**
     * 删除a标签
     * @param str
     * @return
     */
    public static String delectA(String str){
        //不包含.edu的网站
        str = str.replaceAll("<a[^>]*href=[\"'](?<url>[^\"']*?)[\"'][^>]*>(?<text>((?!\\.edu)[\\w\\W])*?)</a>", "");
        return str;
    }

    //删除A标签 保留文本内容
    public static String cleanA(String str){
        String regEx_style = "<a[^>]*href=[\"'](?<url>[^\"']*?)[\"'][^>]*>(?<text>((?!\\.edu)[\\w\\W])*?)</a>";
        Pattern pattern = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        while(matcher.find()){
            str=str.replaceAll(matcher.group(0),matcher.group(2));
        }
        return str;
    }



    public static void main(String[] args)  {
        String str = "<p><img alt=\"\" src=\"http://39.107.247.82:30002/storage/1366*768*34bb57d2da040e6f154b384a6336eed2.jpg\" style=\"height:768px; width:1366px\" />戴安娜</p>\n";


        System.out.println(str);

    }





}
